package zekezang.org;

import common.utils.activity.ActivityQueue;
import common.utils.activity.ActivityUtils;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.Toast;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TabHost.TabSpec;

public class MainActivity extends TabActivity {

//	private ImageButton search_image_button_map = null;
//	private ImageButton search_image_button_weather = null;
//	private ImageButton search_image_button_fly = null;
//	private ImageButton search_image_button_hotel = null;
//
//	private ImageButton type_image_button_website_1 = null;
//	private MyGridViewNoScroll type_website_1 = null;
//
//	private ImageButton type_image_button_website_2 = null;
//	private MyGridViewNoScroll type_website_2 = null;
//
//	private ImageButton type_image_button_website_3 = null;
//	private MyGridViewNoScroll type_website_3 = null;
//
//	private RelativeLayout uuooee = null;
//	private RelativeLayout uuoott = null;
//	private RelativeLayout uuooww = null;
//	private RelativeLayout main_some_website = null;
	private RelativeLayout menu_layout = null;

	private common.utils.activity.MainMenu mainMenu = null;

	private ScrollView myscrollview = null;

	public static TabHost tabHost = null;
	private static String tab_my_navigate = "tab_my_navigate";
	private static String tab_app_center = "tab_app_center";
	private static String navigateString = null;

//	private static List<String> list1 = new ArrayList<String>();
//	private static List<String> list2 = new ArrayList<String>();
//	private static List<String> list3 = new ArrayList<String>();
//	private static List<String> list4 = new ArrayList<String>();
//	private static List<String> list5 = new ArrayList<String>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		ActivityUtils.getInstance().setPortpaitScreenOrientation(this);// .setFullScreenHasTitle(this,
																		// "3G explorer");
		setContentView(R.layout.main_activity);

		ActivityQueue.activityList.add(this);

		menu_layout = (RelativeLayout) findViewById(R.id.menu_layout);
		menu_layout.setVisibility(View.VISIBLE);
		footerSetting();

		myscrollview = (ScrollView) findViewById(R.id.myscrollview);

//		search_image_button_map = (ImageButton) findViewById(R.id.search_image_button_map);
//		search_image_button_weather = (ImageButton) findViewById(R.id.search_image_button_weather);
//		search_image_button_fly = (ImageButton) findViewById(R.id.search_image_button_fly);
//		search_image_button_hotel = (ImageButton) findViewById(R.id.search_image_button_hotel);
//
//		search_image_button_map.setOnClickListener(new View.OnClickListener() {
//			@Override
//			public void onClick(View arg0) {
//				ActivityUtils.getInstance().leftIN_rightOUT_Transition(MainActivity.this, getApplicationContext(),
//						MyMapActivity.class);
//			}
//		});
//
//		if (list1.size() == 0 && list2.size() == 0 && list3.size() == 0) {
//			Intent intent = getIntent();
//			String dataStr = intent.getStringExtra("dataStr");
//			if (dataStr != null) {
//				FileControlUtils.getInstance().getStringList(dataStr, list1, list2, list3, list4, list5);
//			} else {
//				FileControlUtils.getInstance().getStringList(FileControlUtils.getInstance().getMainStringFromFile(),
//						list1, list2, list3, list4, list5);
//			}
//		}
//
//		initWebBigButton();
//
//		initWebHref();
//		initWebSite();

		initSearch();

		Intent intent = getIntent();
		navigateString = intent.getStringExtra("navigate");

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
//		setImagemargin(search_image_button_map);
//		setImagemargin(search_image_button_weather);
//		setImagemargin(search_image_button_fly);
//		setImagemargin(search_image_button_hotel);
		setScrollViewHeight();
		initTab();

	}

	private void initTab() {
		try {
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
					LinearLayout.LayoutParams.WRAP_CONTENT);
			Button LBtn = new Button(this);
			LBtn.setLayoutParams(params);
			LBtn.setBackgroundResource(R.drawable.home_l);
			LBtn.setText(R.string.tab_my_navigate_text);
			LBtn.setClickable(false);

			Button RBtn = new Button(this);
			RBtn.setLayoutParams(params);
			RBtn.setBackgroundResource(R.drawable.home_r);
			RBtn.setText(R.string.tab_app_center_text);
			RBtn.setClickable(false);

			tabHost = getTabHost();
			tabHost.clearAllTabs();
			Intent atabIntent = new Intent(this, TabNavigateActivity.class);
			atabIntent.putExtra("navigateString", navigateString);
			TabSpec aTabSpec = tabHost.newTabSpec(tab_my_navigate).setIndicator(LBtn).setContent(atabIntent);

			TabSpec bTabSpec = tabHost.newTabSpec(tab_app_center).setIndicator(RBtn)
					.setContent(new Intent(this, TabAppCenterActivity.class));

			tabHost.addTab(aTabSpec);
			tabHost.addTab(bTabSpec);

			TabWidget tabWidget = tabHost.getTabWidget();
			int wh[] = ActivityUtils.getInstance().getWidthHight(this);
			for (int i = 0; i < tabWidget.getChildCount(); i++) {
				tabWidget.getChildAt(i).getLayoutParams().width = wh[0] / 2;
			}

			tabHost.setCurrentTab(0);

		} catch (Exception e) {
			Log.e("zekezang", e.getMessage());
			e.printStackTrace();
		}
	}

//	private void initWebBigButton() {
//		search_image_button_weather.setFocusable(true);
//		search_image_button_weather.setFocusableInTouchMode(true);
//		search_image_button_weather.requestFocus();
//		search_image_button_weather.setOnClickListener(new View.OnClickListener() {
//			@Override
//			public void onClick(View arg0) {
//				ActivityUtils.getInstance().leftIN_rightOUT_Transition(MainActivity.this, MainActivity.this,
//						WebviewActivity.class, "uri", list4.get(0).split("\\^")[3]);
//			}
//		});
//		search_image_button_fly.setOnClickListener(new View.OnClickListener() {
//			@Override
//			public void onClick(View arg0) {
//				ActivityUtils.getInstance().leftIN_rightOUT_Transition(MainActivity.this, MainActivity.this,
//						WebviewActivity.class, "uri", list4.get(1).split("\\^")[3]);
//			}
//		});
//		search_image_button_hotel.setOnClickListener(new View.OnClickListener() {
//			@Override
//			public void onClick(View arg0) {
//				ActivityUtils.getInstance().leftIN_rightOUT_Transition(MainActivity.this, MainActivity.this,
//						WebviewActivity.class, "uri", list4.get(2).split("\\^")[3]);
//			}
//		});
//	}
//
//	private void initWebHref() {
//		type_image_button_website_1 = (ImageButton) findViewById(R.id.type_image_button_website_1);
//		uuooee = (RelativeLayout) findViewById(R.id.uuooee);
//		type_website_1 = (MyGridViewNoScroll) findViewById(R.id.type_website_1);
//		type_website_1.setAdapter(new AButton(list1));
//
//		type_image_button_website_1.setOnClickListener(new View.OnClickListener() {
//			@Override
//			public void onClick(View arg0) {
//				if (uuooee.isShown()) {
//					uuooee.setVisibility(View.GONE);
//					type_image_button_website_1.setBackgroundResource(R.drawable.type_close);
//				} else {
//					uuooee.setVisibility(View.VISIBLE);
//					type_image_button_website_1.setBackgroundResource(R.drawable.type_open);
//				}
//			}
//		});
//
//		type_image_button_website_2 = (ImageButton) findViewById(R.id.type_image_button_website_2);
//		uuoott = (RelativeLayout) findViewById(R.id.uuoott);
//		type_website_2 = (MyGridViewNoScroll) findViewById(R.id.type_website_2);
//		type_website_2.setAdapter(new AButton(list2));
//
//		type_image_button_website_2.setOnClickListener(new View.OnClickListener() {
//			@Override
//			public void onClick(View arg0) {
//				if (uuoott.isShown()) {
//					uuoott.setVisibility(View.GONE);
//					type_image_button_website_2.setBackgroundResource(R.drawable.type_close);
//				} else {
//					uuoott.setVisibility(View.VISIBLE);
//					type_image_button_website_2.setBackgroundResource(R.drawable.type_open);
//				}
//			}
//		});
//
//		type_image_button_website_3 = (ImageButton) findViewById(R.id.type_image_button_website_3);
//		uuooww = (RelativeLayout) findViewById(R.id.uuooww);
//		type_website_3 = (MyGridViewNoScroll) findViewById(R.id.type_website_3);
//		type_website_3.setAdapter(new AButton(list3));
//
//		type_image_button_website_3.setOnClickListener(new View.OnClickListener() {
//			@Override
//			public void onClick(View arg0) {
//				if (uuooww.isShown()) {
//					uuooww.setVisibility(View.GONE);
//					type_image_button_website_3.setBackgroundResource(R.drawable.type_close);
//				} else {
//					uuooww.setVisibility(View.VISIBLE);
//					type_image_button_website_3.setBackgroundResource(R.drawable.type_open);
//				}
//			}
//		});
//	}
//
//	private void initWebSite() {
//		main_some_website = (RelativeLayout) findViewById(R.id.main_some_website);
//		String[] ss = new String[4];
//		Button button = null;
//		for (String s : list5) {
//			ss = s.split("\\^");
//			String textButton = ss[2];
//			final String uri = ss[3];
//			String colorText = ss[1];
//			button = new Button(MainActivity.this);
//			button.setBackgroundDrawable(getApplicationContext().getResources().getDrawable(R.drawable.link_button));
//			button.setText(textButton);
//			button.setTextSize(12f);
//
//			if (colorText.equals("1")) {
//				button.setTextColor(Color.BLACK);
//			} else if (colorText.equals("2")) {
//				button.setTextColor(Color.rgb(143, 184, 219));
//			} else if (colorText.equals("3")) {
//				button.setTextColor(Color.RED);
//			} else {}
//
//			//跳转判断
//			if (!uri.equals("#") && uri.endsWith(".apk")) {
//				button.setOnClickListener(new View.OnClickListener() {
//					@Override
//					public void onClick(View arg0) {
//						Uri url = Uri.parse(uri);
//						Intent intent = new Intent(Intent.ACTION_VIEW, url);
//						startActivity(intent);
//						ActivityUtils.getInstance().exitClient(getApplicationContext());
//					}
//				});
//			}else{
//				
//			}
//			main_some_website.addView(button);
//		}
//	}

	private EditText search_edit_text = null;
	private Button search_button_through = null;
	private Button search_button_search = null;
	private String regex = "((http://|https://){0,1}(\\w+[\\.]))?(\\w+[\\.])?(\\w+)?([[\\.](\\w+)])+(/|.)*";

	private void initSearch() {
		search_edit_text = (EditText) findViewById(R.id.search_edit_text);

		search_edit_text.setOnFocusChangeListener(new View.OnFocusChangeListener() {
			@Override
			public void onFocusChange(View view, boolean b) {
				if (b) {
					if (getResources().getString(R.string.edit_text).equals(
							search_edit_text.getText().toString().trim())) {
						search_edit_text.setText("");
					}
				}
			}
		});

		search_button_through = (Button) findViewById(R.id.search_button_through);
		search_button_search = (Button) findViewById(R.id.search_button_search);

		search_button_through.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				String uri = search_edit_text.getText().toString().trim();
				if (uri.length() > 0 && uri.matches(regex)) {
					if (!uri.startsWith("http")) {
						uri = "http://" + uri;
					}
					ActivityUtils.getInstance().leftIN_rightOUT_Transition(MainActivity.this, getApplicationContext(),
							WebviewActivity.class, "uri", uri);
				} else if (uri.length() > 0 && !getResources().getString(R.string.edit_text).equals(uri)) {
					String url = "http://web.roboo.com/direct.rob?query=" + uri + "&direct=直达";
					ActivityUtils.getInstance().leftIN_rightOUT_Transition(MainActivity.this, getApplicationContext(),
							WebviewActivity.class, "uri", url);
				} else {
					Toast.makeText(MainActivity.this, R.string.no_uri, Toast.LENGTH_SHORT).show();
				}
			}
		});
		search_button_search.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				String searchText = search_edit_text.getText().toString().trim();
				if (searchText.length() > 0 && !getResources().getString(R.string.edit_text).equals(searchText)) {
					String uri = "http://web.roboo.com/direct.rob?query=" + searchText + "&direct=搜索";
					ActivityUtils.getInstance().leftIN_rightOUT_Transition(MainActivity.this, getApplicationContext(),
							WebviewActivity.class, "uri", uri);
				} else {
					Toast.makeText(MainActivity.this, R.string.no_uri, Toast.LENGTH_SHORT).show();
				}
			}
		});
	}

	private void footerSetting() {
		mainMenu = common.utils.activity.MainMenu.getInstance(this, this, menu_layout, null, "148", null);
		// menu向上键
		ImageButton menu_up_button = (ImageButton) menu_layout.findViewById(R.id.menu_up_button);
		menu_up_button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mainMenu.responseOnKeyDown();
			}
		});
		// menu主页
		final ImageButton menu_home_button = (ImageButton) menu_layout.findViewById(R.id.menu_home_button);
		menu_home_button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// ActivityUtils.getInstance().to_home_Transition(MainActivity.this,
				// getApplicationContext(),MainActivity.this);
			}
		});
		// menu后退
		final ImageButton menu_back_button = (ImageButton) menu_layout.findViewById(R.id.menu_back_button);
		menu_back_button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mainMenu.responseOnTouch();
				ActivityUtils.getInstance().back_leftOUT_rightIN_Transition(MainActivity.this, getApplicationContext());
			}
		});
		// menu前进
		final ImageButton menu_go_button = (ImageButton) menu_layout.findViewById(R.id.menu_go_button);
		menu_go_button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mainMenu.responseOnTouch();
				ActivityUtils.getInstance().forward_leftOUT_rightIN_Transition(MainActivity.this,
						getApplicationContext());
			}
		});
	}

	private void setScrollViewHeight() {
		int[] p = ActivityUtils.getInstance().getWidthHight(this);
		RelativeLayout.LayoutParams params = (LayoutParams) myscrollview.getLayoutParams();
		params.height = p[1] - 30 - 35 - 30;
	}

//	private void setImagemargin(ImageButton imageButton) {
//		int[] p = ActivityUtils.getInstance().getWidthHight(this);
//		RelativeLayout.LayoutParams params = (LayoutParams) imageButton.getLayoutParams();
//		params.leftMargin = (p[0] - 60 * 4) / 5;
//	}
//
//	class AButton extends BaseAdapter {
//		private List<String> list = null;
//
//		public AButton(List<String> list) {
//			this.list = list;
//		}
//
//		@Override
//		public int getCount() {
//			return list.size();
//		}
//
//		@Override
//		public Object getItem(int i) {
//			return list.get(i);
//		}
//
//		@Override
//		public long getItemId(int i) {
//			return i;
//		}
//
//		Button button = null;
//		String str = null;
//		String[] strs = null;
//
//		@Override
//		public View getView(int i, View view, ViewGroup viewGroup) {
//			str = list.get(i);
//			strs = str.split("\\^");
//			String textButton = strs[2];
//			final String uri = strs[3];
//			String colorText = strs[1];
//			button = new Button(MainActivity.this);
//			button.setBackgroundDrawable(getApplicationContext().getResources().getDrawable(R.drawable.link_button));
//			button.setText(textButton);
//			button.setTextSize(12f);
//
//			if (colorText.equals("1")) {
//				button.setTextColor(Color.BLACK);
//			} else if (colorText.equals("2")) {
//				button.setTextColor(Color.rgb(143, 184, 219));
//			} else if (colorText.equals("3")) {
//				button.setTextColor(Color.RED);
//			} else {}
//
//			if (!uri.equals("#")) {
//				button.setOnClickListener(new View.OnClickListener() {
//					@Override
//					public void onClick(View arg0) {
//						ActivityUtils.getInstance().leftIN_rightOUT_Transition(MainActivity.this, MainActivity.this,
//								WebviewActivity.class, "uri", uri);
//					}
//				});
//			}
//
//			return button;
//		}
//
//	}

}
